import base64
from io import BytesIO

import qrcode


class QRGenerator:
    """二维码生成工具类"""
    def generate_qr_code(self, url):
        """生成指定URL的二维码图片
        Args:
            url (str): 需要编码的URL地址
        Returns:
            str: Base64编码的二维码图片字符串
        """
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data(url)
        qr.make(fit=True)
        img = qr.make_image(fill_color="black", back_color="white")

        img_io = BytesIO()
        img.save(img_io, "PNG")
        img_io.seek(0)
        return base64.b64encode(img_io.getvalue()).decode('utf-8')

import qrcode
import io
import base64

class QRGenerator:
    def generate(self, url):
        """生成二维码并返回Base64编码"""
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data(url)
        qr.make(fit=True)

        img = qr.make_image(fill_color="black", back_color="white")
        buffered = io.BytesIO()
        img.save(buffered, format="PNG")
        img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')

        return f"data:image/png;base64,{img_base64}"
